package cn.wen.yinxiu.product.controller;

import java.util.Arrays;
import java.util.Map;

import cn.wen.yinxiu.common.convention.result.Results;
import cn.wen.yinxiu.common.to.product.SkuInfoTO;
import cn.wen.yinxiu.product.vo.AddSkuInfoVO;
import cn.wen.yinxiu.product.vo.SkuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yinxiu.product.entity.SkuInfoEntity;
import cn.wen.yinxiu.product.service.SkuInfoService;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.convention.result.ResultEntity;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @ClassName: SkuInfoController
 * @Author 小飞
 * @Email coderwen22@163.com
 * @Date 2023-05-22 09:55:17
 * @Description: sku信息的Controller层
 */
@RestController
@RequestMapping("/web")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 对作品添加商品信息
     */
    @PostMapping("/api/product/save")
    public ResultEntity save(@RequestPart("imageList")MultipartFile[] imageList,
                             @RequestPart("skuDescList") MultipartFile[] skuDescList,
                             @RequestParam("skuInfo") String skuInfo){
        boolean res = skuInfoService.saveSkuInfo(imageList, skuDescList, skuInfo);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 对作品删除商品信息
     */
    @PostMapping("/api/product/delete")
    public ResultEntity deleteSkuInfProductId(@RequestParam("productId") Long productId,
                             @RequestParam("worksId") Long worksId){
        boolean res = skuInfoService.deleteSkuInfoByWorks(productId, worksId);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 对照片修改了单元的
     */
    @PostMapping("/api/product/update")
    public ResultEntity updateSkuInfoProductId(@RequestPart("imageList")MultipartFile[] imageList,
                                              @RequestPart("skuDescList") MultipartFile[] skuDescList,
                                              @RequestParam("skuInfo") String skuInfo){
        boolean res = skuInfoService.updateSkuInfoByWorks(imageList, skuDescList, skuInfo);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 通过商品Id 获取商品的信息
     * @param productId
     * @return
     */
    @GetMapping("/api/product/info")
    public ResultEntity getProductInfo(@RequestParam("productId") Long productId) {
        SkuInfoVO res = skuInfoService.getSkuInfoByProductId(productId);
        return Results.success(res);
    }

    /**
     * 通过用户ID获取用户的地址列表
     * @return
     */
    @GetMapping("/web/api/product/simple/info")
    ResultEntity<SkuInfoTO> getSkuInfoById(@RequestParam("skuId") Long skuId) {
        SkuInfoTO res = skuInfoService.getSkuInfoById(skuId);
        return Results.success(res);
    }

}
